﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace CSharpWin_JD.CaptureImage
{
    /* 作者：Starts_2000
     *      （涂剑凯修改 http://www.cnblogs.com/bdstjk/）
     * 日期：2009-09-08
     * 网站：http://www.csharpwin.com CS 程序员之窗。
     * 你可以免费使用或修改以下代码，但请保留版权信息。
     * 具体请查看 CS程序员之窗开源协议（http://www.csharpwin.com/csol.html）。
     */
    public class ColorLabel : Control
    {
        #region Fields

        private Color _showColor = Color.FromArgb(230, 235, 243);
        private Color _borderColor = Color.FromArgb(128, 128, 128);
        private Color _selectedColor = Color.FromArgb(255, 255, 255);
        private bool _showHover = true;
        private bool _isMouseEnter = false;
        
        #endregion

        #region Constructors

        public ColorLabel()
            : base()
        {
            InitializeComponent();
            SetStyles();
        }

        #endregion

        #region Properties

        [DefaultValue(typeof(Color), "230, 235, 243")]
        public System.Drawing.Color ShowColor
        {
            get { return _showColor; }
            set { _showColor = value; Invalidate(); }
        }

        [DefaultValue(typeof(Color), "128, 128, 128")]
        public System.Drawing.Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; Invalidate(); }
        }

        [DefaultValue(typeof(Color), "255, 255, 255")]
        public System.Drawing.Color SelectedColor
        {
            get { return _selectedColor; }
            set { _selectedColor = value; Invalidate(); }
        }

        [DefaultValue(typeof(bool), "true")]
        public bool ShowHover
        {
            get { return _showHover; }
            set { _showHover = value; }
        }

        protected override Size DefaultSize
        {
            get { return new Size(15, 15); }
        }

        #endregion

        #region Private Methods

        private void SetStyles()
        {
            base.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw, true);
            base.UpdateStyles();
        }

        #endregion

        #region OverideMethods

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            Rectangle rect = ClientRectangle;
            using (SolidBrush brush = new SolidBrush(ShowColor))
            {
                g.FillRectangle(brush, rect.Left+2, rect.Top+2, rect.Width-4, rect.Height-4);
            }
            if (ShowHover)
            {
                if (_isMouseEnter)
                {
                    g.DrawRectangle(new Pen(SelectedColor), rect.Left+1, rect.Top+1, rect.Width-3, rect.Height-3);
                    g.DrawRectangle(new Pen(BorderColor), rect.Left, rect.Top, rect.Width-1, rect.Height-1);
                }
                else
                {
                    g.DrawRectangle(new Pen(BorderColor), rect.Left+1, rect.Top+1, rect.Width-3, rect.Height-3);
                }
            }
            else 
            {
                g.DrawRectangle(new Pen(BorderColor), rect.Left, rect.Top, rect.Width-1, rect.Height-1);
            }
        }

        #endregion

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ColorLabel
            // 
            this.MouseEnter += new System.EventHandler(this.ColorLabel_MouseEnter);
            this.MouseLeave += new System.EventHandler(this.ColorLabel_MouseLeave);
            this.ResumeLayout(false);

        }

        private void ColorLabel_MouseEnter(object sender, EventArgs e)
        {
            _isMouseEnter = true;
            Invalidate();
        }

        private void ColorLabel_MouseLeave(object sender, EventArgs e)
        {
            _isMouseEnter = false;
            Invalidate();
        }
    }
}
